package top.dingwen.io.treasure.websocket.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.dingwen.io.treasure.webplus.result.Result;
import top.dingwen.io.treasure.webplus.web.controller.BaseViewController;
import top.dingwen.io.treasure.websocket.core.holder.WebSocketSessionHolder;
import top.dingwen.io.treasure.websocket.util.WebSocketUtils;

import java.util.Set;


/**
 * WebSocket场景API
 *
 * @author dingwen
 * @since 2023/5/22 11:11
 */
@RestController
@Slf4j
@RequestMapping("common/websockets")
public class WebSocketIndexController implements BaseViewController {
    /**
     * 获取所有在线的用户
     *
     * @return {@link Result }<{@link Set }<{@link Long }>> 在线的用户ids
     */
    @GetMapping
    public Result<Set<String>> getAllOnlineUsers() {
        return success(WebSocketSessionHolder.getSessionKeysAll());
    }

    /**
     * 发送消息
     *
     * @param loginId  登录编号
     * @param message 消息
     * @return {@link Result }<{@link String }> 结果
     */
    @PostMapping
    public Result<String> sendMessage(@RequestParam String loginId,
                                      @RequestParam String message) {
        WebSocketUtils.sendMessage(loginId, message);
        return success();
    }

    /**
     * 群发消息
     *
     * @param message 消息
     * @return {@link Result }<{@link String }> 结果
     */
    @PostMapping("/batch")
    public Result<String> sendMessages(@RequestParam String message) {
        WebSocketUtils.sendMessages(message);
        return success();
    }

}
